//
//  ShowDataViewController.m
//  Demo1013TableViewSQLite
//
//  Created by yang on 10-10-12.
//  Copyright 2010 www.1000phone.com. All rights reserved.
//

#import "ShowDataViewController.h"


@implementation ShowDataViewController
@synthesize myTableView;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


- (NSString *) databasePath
{
	NSArray *path = NSSearchPathForDirectoriesInDomains(
														NSDocumentDirectory, 
														NSUserDomainMask, 
														YES);
	NSString *pathname = [path objectAtIndex:0];
	NSLog(@"path name is %@", pathname);
	/* 上面得到了 
	 NSString *path = [NSHomeDirectory() 
	 stringByAppendingPathComponent:@”Documents"];
	 得到 应用主目录下面的 Documents目录
	 / 就是一个 PathComponent
	 */
	return [pathname 
			stringByAppendingPathComponent:@"mydata.sqlite3"];
}

- (NSInteger) getTotalCount
{
	/* 从数据库中读取到底又多少行数据 */
	/* 打开数据库 */
	if (sqlite3_open([[self databasePath] UTF8String],
					&database) != SQLITE_OK)
	{
		NSLog(@"error for open");
	}
	NSString *countSQL= @"select count(*) from MyTableUserInfo";
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2(database,
				[countSQL UTF8String],
				-1, &statement, NULL) == SQLITE_OK)
	{
		while (sqlite3_step(statement) == SQLITE_ROW) {
			int i = sqlite3_column_int(statement, 0);
			/* 取得当前行的第0列 */
			sqlite3_finalize(statement);
			NSLog(@"count %d", i);
			return i;
		}
	}
	
	return 0;
}
- (NSInteger) tableView:(UITableView *)tableView
	numberOfRowsInSection:(NSInteger)section
{
	int count = [self getTotalCount];
	if (count == 0) {
		return 1;
	}
	return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *CellID = @"BaseCell";
	UITableViewCell *cell = [tableView 
				dequeueReusableCellWithIdentifier:CellID];
	
	if (cell == nil) {
		cell = [[UITableViewCell alloc]
					initWithStyle:UITableViewCellStyleDefault
				reuseIdentifier:CellID];
		[cell autorelease];
	}
	int count = [self getTotalCount];
	if (count == 0) {
		cell.textLabel.text = @"No Data";
		cell.textLabel.textAlignment = UITextAlignmentCenter;
		/* 当数据库没有数据时候，显示No Data
		 并且文本居中 */
		return cell;
	}
	
	int row = [indexPath row];
	if (sqlite3_open([[self databasePath] UTF8String],
					 &database) != SQLITE_OK)
	{
		NSLog(@"error for open");
	}
	NSString *selectSQL = @"select * from MyTableUserInfo";
	sqlite3_stmt *statement;
	int count2 = 0;
	if (sqlite3_prepare_v2(database, 
				[selectSQL UTF8String],
				-1, &statement, NULL) == SQLITE_OK)
	{
		while (sqlite3_step(statement) == SQLITE_ROW) {
			NSLog(@"count2 is %d", count2);
			if (count2++ == row) {
				int ID = sqlite3_column_int(
							statement, 0);
				const char *name2 = sqlite3_column_text(
							statement, 1);
				NSString *name3 = [NSString stringWithUTF8String:
								   name2];
				const unsigned char *age = sqlite3_column_text(
							statement, 2);
				NSString *age3 = [NSString stringWithUTF8String:
								  age];
				const unsigned char *gender = sqlite3_column_text(
							statement, 3);
				NSString *gender3 = [NSString stringWithUTF8String:
									 gender];				
				cell.textLabel.text = 
				[NSString stringWithFormat:@"ID %d %@ %@ %@",
				 ID, name3, age3, gender3];
		
			}
		}
	}
	
	
	return cell;						 
}

@end
